package LeetcodeTest_twenty_eight;
//1286.字母组合迭代器
import java.util.ArrayList;

public class Solution
{
    public static void main(String[] args)
    {
        CombinationIterator obj=new CombinationIterator("abc",2);
        boolean flag= obj.hasNext();
        while(flag)
        {
            String a=obj.next();
            System.out.println(a);
            flag=obj.hasNext();
            System.out.println(flag);
        }
    }
}
class CombinationIterator
{
    public int count;
    public ArrayList<String> result;
    public String target;
    public CombinationIterator(String characters, int combinationLength)
    {
        result=new ArrayList<String>();
        count=0;
        target=characters;
        difs(characters,combinationLength);
    }
    public String next()
    {
        if(count<=result.size())
        {
            return result.get(count - 1);
        }
        else
            return null;
    }

    public boolean hasNext()
    {
        if(count<=result.size())
        {
            count++;
            return true;
        }
        else
            return false;
    }
    public void difs(String characters,int combinationLength)
    {
        StringBuffer answer=new StringBuffer();
        backtrace(answer,combinationLength,0);
    }
    public void backtrace(StringBuffer answer,int combinationLength,int index)
    {
        if(answer.length()==combinationLength)
        {
            result.add(answer.toString());
            return;
        }
        for(int i=index;i<target.length();i++)
        {
            answer.append(target.charAt(i));
            backtrace(answer,combinationLength,i+1);
            answer.deleteCharAt(answer.length()-1);
        }
    }
}